package com.Service;

import com.Dao.OrderEngineerDao;
import com.Dao.OrdersDao;
import com.POJO.OrderEngineer;
import com.POJO.Orders;
import com.Service.Imp.OrderEngineerServiceImp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderEngineerService extends ServiceImpl<OrderEngineerDao, OrderEngineer> implements OrderEngineerServiceImp {
    @Autowired
    private OrderEngineerDao orderEngineerDao;
    @Autowired
    private OrdersDao ordersDao;

    /**
     * 查找与本人有关的等待中订单数据
     * @param flag
     * @param tel
     * @return
     */
    public List<OrderEngineer> FindOrders(int flag, String tel){
        QueryWrapper<OrderEngineer> orderEngineerQueryWrapper=new QueryWrapper<>();
        if(flag==1)
        orderEngineerQueryWrapper.eq("user_tel",tel);
        else
        orderEngineerQueryWrapper.eq("engineer_tel",tel);
        orderEngineerQueryWrapper.eq("state",0);
        return orderEngineerDao.selectList(orderEngineerQueryWrapper);
    }
    public int EngineerAddMoney(String uuid, String tel,BigDecimal money){
        UpdateWrapper<OrderEngineer> updateWrapper=new UpdateWrapper<>();
        OrderEngineer orderEngineer=new OrderEngineer();
        updateWrapper.set("state",1);
        updateWrapper.set("money",money);
        updateWrapper.eq("order_uuid",uuid);
        updateWrapper.eq("engineer_tel",tel);
        UpdateWrapper<Orders> updateWrapper1=new UpdateWrapper<>();
        Orders orders=new Orders();
        updateWrapper1.set("state",1);
        updateWrapper1.eq("order_uuid",uuid);
        ordersDao.update(orders,updateWrapper1);
        return orderEngineerDao.update(orderEngineer,updateWrapper);
    }
    public int EngineerDelMoney(String uuid, String tel){
        UpdateWrapper<OrderEngineer> updateWrapper=new UpdateWrapper<>();
        OrderEngineer orderEngineer=new OrderEngineer();
        updateWrapper.set("state",0);
        BigDecimal money=new BigDecimal(0);
        updateWrapper.set("money",money);
        updateWrapper.eq("order_uuid",uuid);
        updateWrapper.eq("engineer_tel",tel);
        UpdateWrapper<Orders> updateWrapper1=new UpdateWrapper<>();
        Orders orders=new Orders();
        updateWrapper1.set("state",0);
        updateWrapper1.eq("order_uuid",uuid);
        ordersDao.update(orders,updateWrapper1);
        return orderEngineerDao.update(orderEngineer,updateWrapper);
    }
}
